package sort;

import java.util.Arrays;

/**
 * @author XY
 * @version 1.0
 * @date 2022/2/15 9:02
 * @Description
 */
public class Leet747 {

    public static int dominantIndex(int[] nums) {
        if(nums.length==1){
            return 0;
        }
        int[] ints = Arrays.copyOf(nums, nums.length);
        Arrays.sort(ints);
        int max = ints[ints.length-1];
        int index = -1;
        for (int i = 0; i < nums.length; i++) {
            if(max==nums[i]){
                index = i;
            }
            if(max!=nums[i]&&!(max>=2*nums[i])){
                return -1;
            }
        }
        return index;
    }

    //最大值m1,次大值m2 ，m1>=2*m2 即可完成
    public int dominantIndex1(int[] nums) {
        int m1 = -1, m2 = -1;
        int index = -1;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > m1) {
                m2 = m1;
                m1 = nums[i];
                index = i;
            } else if (nums[i] > m2) {
                m2 = nums[i];
            }
        }
        return m1 >= m2 * 2 ? index : -1;
    }

    public static void main(String[] args) {
        int[] arr= {1,2,3,4};
        System.out.println(dominantIndex(arr));
    }
}
